From 3a87d0067ce036152900514901e0959dfe5200a4 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 9 Sep 2015 15:44:43 -0700 Subject: [PATCH] Use a stamp file to protect against corrupt checkouts We already take this strategy for extracting tarballs from crates.io for example, so this just applies the same strategy to checkouts of git repos. Closes #1979 --- src/cargo/sources/git/utils.rs | 18 ++++++++++++++++-- tests/test_cargo_compile.rs | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index 51f69a6d2..f51b9dc97 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -1,6 +1,6 @@ use std::fmt; use std::path::{Path, PathBuf}; -use std::fs; +use std::fs::{self, File}; use rustc_serialize::{Encodable, Encoder}; use url::Url; @@ -267,7 +267,10 @@ impl<'a> GitCheckout<'a> { fn is_fresh(&self) -> bool { match self.repo.revparse_single("HEAD") { - Ok(head) => head.id().to_string() == self.revision.to_string(), + Ok(ref head) if head.id() == self.revision.0 => { + // See comments in reset() for why we check this + fs::metadata(self.location.join(".cargo-ok")).is_ok() + } _ => false, } } @@ -282,9 +285,20 @@ impl<'a> GitCheckout<'a> { } fn reset(&self) -> CargoResult<()> { + // If we're interrupted while performing this reset (e.g. we die because + // of a signal) Cargo needs to be sure to try to check out this repo + // again on the next go-round. + // + // To enable this we have a dummy file in our checkout, .cargo-ok, which + // if present means that the repo has been successfully reset and is + // ready to go. Hence if we start to do a reset, we make sure this file + // *doesn't* exist, and then once we're done we create the file. + let ok_file = self.location.join(".cargo-ok"); + let _ = fs::remove_file(&ok_file); info!("reset {} to {}", self.repo.path().display(), self.revision); let object = try!(self.repo.find_object(self.revision.0, None)); try!(self.repo.reset(&object, git2::ResetType::Hard, None)); + try!(File::create(ok_file)); Ok(()) } diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 62dfd76f2..482a6dee4 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -1508,7 +1508,7 @@ test!(cargo_platform_specific_dependency { execs().with_status(0)); assert_that(&p.bin("foo"), existing_file()); - assert_that(p.cargo_process("test"), + assert_that(p.cargo("test"), execs().with_status(0)); }); -- 2.30.2